"""
Global registry instances for different component types.

This module provides pre-configured registry instances for common
component types in the continual learning framework using the base Registry system.
"""

from continuallearning.interfaces import (
    ModelInterface,
    AdapterInterface,
    CallbackInterface,
    ComponentInterface,
    MethodInterface,
    BackboneInterface,
    MultiHeadInterface,
    HookBasedAdapterInterface,
    HookManagerInterface,
    HookFactoryInterface,
    CombinerInterface,
    RouterInterface,
    TaskAwareHookInterface,
)
from continuallearning.events.interfaces import StrategyEventHandlerInterface
from .base import Registry
from .decorators import create_component_decorator
from typing import Dict, List, Any, Union

# 注册表实例 - 使用复数形式，首字母大写（优雅简洁）
Strategies = Registry[StrategyEventHandlerInterface](
    name="Strategies", interface_type=StrategyEventHandlerInterface
)

Models = Registry[ModelInterface](
    name="Models", interface_type=ModelInterface
)

Adapters = Registry[AdapterInterface](
    name="Adapters", interface_type=AdapterInterface
)

Callbacks = Registry[CallbackInterface](
    name="Callbacks", interface_type=CallbackInterface
)

Methods = Registry[MethodInterface](
    name="Methods", interface_type=MethodInterface
)

Backbones = Registry[BackboneInterface](
    name="Backbones", interface_type=BackboneInterface
)

Heads = Registry[MultiHeadInterface](
    name="Heads", interface_type=MultiHeadInterface
)

# Hook相关注册表
Hooks = Registry[TaskAwareHookInterface](
    name="Hooks", interface_type=TaskAwareHookInterface
)

HookFactories = Registry[HookFactoryInterface](
    name="HookFactories", interface_type=HookFactoryInterface
)

HookManagers = Registry[HookManagerInterface](
    name="HookManagers", interface_type=HookManagerInterface
)

HookAdapters = Registry[HookBasedAdapterInterface](
    name="HookAdapters", interface_type=HookBasedAdapterInterface
)

Combiners = Registry[CombinerInterface](
    name="Combiners", interface_type=CombinerInterface
)

Routers = Registry[RouterInterface](
    name="Routers", interface_type=RouterInterface
)

# 全局注册表
Components = Registry[ComponentInterface](
    name="Components", interface_type=ComponentInterface
)

# 装饰器 - 使用简洁的小写名称
strategy = create_component_decorator(Strategies)
model = create_component_decorator(Models)
adapter = create_component_decorator(Adapters)
callback = create_component_decorator(Callbacks)
method = create_component_decorator(Methods)
backbone = create_component_decorator(Backbones)
head = create_component_decorator(Heads)
hook = create_component_decorator(Hooks)
hook_factory = create_component_decorator(HookFactories)
hook_manager = create_component_decorator(HookManagers)
hook_adapter = create_component_decorator(HookAdapters)
combiner = create_component_decorator(Combiners)
router = create_component_decorator(Routers)
component = create_component_decorator(Components)

# 注册表管理器
class RegistryManager:
    """Centralized manager for all registry instances."""

    def __init__(self):
        self._registries = {
            "strategies": Strategies,
            "models": Models,
            "adapters": Adapters,
            "callbacks": Callbacks,
            "methods": Methods,
            "backbones": Backbones,
            "heads": Heads,
            "hooks": Hooks,
            "hook_factories": HookFactories,
            "hook_managers": HookManagers,
            "hook_adapters": HookAdapters,
            "combiners": Combiners,
            "routers": Routers,
            "components": Components,
        }

    def get_registry(self, name: str) -> Registry:
        """Get a registry by name."""
        registry = self._registries.get(name)
        if registry is None:
            raise KeyError(
                f"Registry '{name}' not found. Available registries: {list(self._registries.keys())}"
            )
        return registry

    def register_registry(self, name: str, registry: Registry) -> None:
        """Register a new registry."""
        self._registries[name] = registry

    def get_all_components(self) -> Dict[str, List[str]]:
        """Get all components from all registries."""
        result = {}
        for name, registry in self._registries.items():
            result[name] = registry.list_registered()
        return result

    def get_statistics(self) -> Dict[str, Any]:
        """Get statistics from all registries."""
        return {
            name: {"component_count": len(registry)}
            for name, registry in self._registries.items()
        }

    @property
    def all_registries(self) -> Dict[str, Registry]:
        """Get all registries as a dict."""
        return self._registries.copy()


# 全局注册表管理器实例
registries = RegistryManager()
